package login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-08-08
 * Time: 15:55
 */
//这个用来动态的生成主页面
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        此处禁止创建会话，如果没找到，认为用户是未登录状态
        HttpSession session = req.getSession(false);

        if(session == null) {
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("当前用户未登录");
            return;
        }

        String username = (String)session.getAttribute("username");

        if(username == null) {
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("当前输入的用户名或密码不能为空");
            return;
        }

//        上述检查都ok 接下来就直接生成一个动态页面
        resp.setContentType("text/html; charset=utf8");
        resp.getWriter().write("welcome " + username);
    }
}
